package com.heer.gymnasium.wxapi;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.heer.api.constants.StaticValue;
import com.heer.gymnasium.activity.MainActivity;
import com.heer.gymnasium.activity.MyWalletActivity;
import com.heer.gymnasium.activity.PaySucceedActivity;
import com.heer.gymnasium.event.BaseEvent;
import com.qict.xinba.R;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import static com.heer.gymnasium.event.EventConstant.CANCEL_ORDER;
import static com.heer.gymnasium.event.EventConstant.CREATE_ORDER;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, "wxcd391c2ab8b96890");
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {

	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if(resp.errCode==0){
				Toast.makeText(getApplicationContext(), "支付成功", Toast.LENGTH_SHORT).show();
				if (StaticValue.PayType==0)
				{
					PaySucceedActivity.StartPaySucceed(this);
				}else {
					startActivity(new Intent(this, MyWalletActivity.class));
				}

			} else {
				Toast.makeText(getApplicationContext(), "支付失败", Toast.LENGTH_SHORT).show();
				finish();
			}
		}
	}
}